#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[100210],c[100210],q[50210][100],zs[100210];
int solve(vector<pair<int,int> >s,int r){
	vector<pair<int,int> >ss;
	if(s.size()==1)return s[0].second;
	for(int i=0;i<s.size();i+=2){
		if(q[i/2][r-1]==0){
			if(s[i].first>=r)ss.push_back(s[i]);
			else ss.push_back(ss[i+1]);
		}
		else{
			if(s[i+1].first>=r)ss.push_back(s[i+1]);
			else ss.push_back(ss[i]);
		}
	}
	return solve(ss,r+1);
}
signed main(){
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=0;i<m;i++)
		cin>>c[i];
	int ii=0;
	for(;;ii++)
		if(pow(2,ii)>=n)break;
	for(int i=0;i<ii;i++){
		string x;
		cin>>x;
		for(int o=0;o<x.size();o++)
			q[o][i]=x[o]-'0';
	}
	int t;
	cin>>t;
	while(t--){
		int x1,x2,x3,x4,ans=0;
		cin>>x1>>x2>>x3>>x4;
		for(int i=1;i<=n;i+=4)
			zs[i]=a[i]^x1;
		for(int i=2;i<=n;i+=4)
			zs[i]=a[i]^x2;
		for(int i=3;i<=n;i+=4)
			zs[i]=a[i]^x3;
		for(int i=4;i<=n;i+=4)
			zs[i]=a[i]^x4;
		for(int i=0;i<m;i++){
			vector<pair<int,int> >x;
			for(int j=1;j<=c[i];j++)
				x.push_back(make_pair(zs[j],j));
			ans^=(i^solve(x,1));
		}
		cout<<ans<<'\n';
	}
	return 0;
}